package view.Bullet
{
	import flash.display.Bitmap;
	import starling.core.Starling;
	import starling.display.Image;
	import starling.display.Sprite;
	import starling.events.Event;
	import starling.textures.Texture;
	
	import utils.LevelConfigXmlUtils;
	import utils.Vector2D;
	
	public class BulletView extends Sprite
	{
        /**子弹图片*/
        private var _bullet:Image;
        /**子弹ID*/
        private var _bulletId:uint;
        /**子弹NAME*/
        private var _bulletName:String;
		private var _position:Vector2D;
		private var _velocity:Vector2D;
		private var _speed:Number;
        /**子弹价格*/
		private var _price:uint;
        /**子弹概率*/
		private var _gailv:Number;
		public function BulletView(bulletId:uint)
		{
			super();
            _bulletId = bulletId;
            addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
		}

        private function onAddedToStage(e:Event):void{
            removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
            init();
        }

        private function init():void{
			_position = new Vector2D();
			_velocity = new Vector2D();
			
			var list:XMLList = LevelConfigXmlUtils.getInstance().xml.bulletConfig.child("group").child("item");
			var i:uint;
			for(i=0;i<list.length();i++){
				if(list[i].@id == _bulletId.toString()){
					bulletName = list[i].@name;   
					_speed = list[i].@speed;
					_gailv = list[i].@gailv;
                    _price = list[i].@price;
					break;
				}
			}
			_bullet = new Image(Assets.getAtlas().getTexture(bulletName));
			addChild(_bullet);
			_bullet.pivotX = _bullet.width >> 1;
			_bullet.pivotY = _bullet.height >> 1;
			
			this.touchable = false;
        }
		
		public function update():void{
			_position = _position.add(_velocity);
			x = _position.x;
			y = _position.y;
		}
		
		public function destroy():void{
			_bullet.removeFromParent(true);
			_bullet.dispose();
		}
		
/*----------------------------------getter and setter---------------------------------------*/
        public function get bulletId():uint{
            return _bulletId;
        }

        public function set bulletId(value:uint):void{
            _bulletId = value;
        }

		public function get bulletName():String
		{
			return _bulletName;
		}

		/**
		 * @param value+"1" 则为提示用途的子弹，相对较大，在纹理xml中名字后带1
		 * @param value+"2" 则为射击用途的子弹，相对娇小，在纹理xml中名字后带2
		 * */
		public function set bulletName(value:String):void
		{
			_bulletName = value + "2";
		}

		public function get position():Vector2D
		{
			return _position;
		}

		public function set position(value:Vector2D):void
		{
			_position = value;
			this.x = _position.x;
			this.y = _position.y;
		}

		public function get velocity():Vector2D
		{
			return _velocity;
		}

		public function set velocity(value:Vector2D):void
		{
			_velocity = value;
		}
		
		/**子弹速度*/
		public function get speed():Number
		{
			return _speed;
		}

		public function set speed(value:Number):void
		{
			_speed = value;
		}

		public function get price():uint
		{
			return _price;
		}

		public function set price(value:uint):void
		{
			_price = value;
		}

		public function get gailv():Number
		{
			return _gailv;
		}

		public function set gailv(value:Number):void
		{
			_gailv = value;
		}

		
	}
}
